package com.ssm.auth.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.ssm.auth.common.enums.IsDeletedFlagEnum;
import com.ssm.auth.domain.bo.AuthRoleBO;
import com.ssm.auth.domain.convert.AuthRoleBoConvert;
import com.ssm.auth.domain.service.AuthRoleDomainService;
import com.ssm.auth.infra.basic.entity.AuthRole;
import com.ssm.auth.infra.basic.service.AuthRoleService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Log4j2
public class AuthRoleDomainServiceImpl implements AuthRoleDomainService {

    @Resource
    private AuthRoleService authRoleService;

    /**
     * 新增角色
     * 只单纯增加角色，不需要同步redis
     * @param authRoleBO
     * @return
     */
    @Override
    public Boolean add(AuthRoleBO authRoleBO) {
        if(log.isInfoEnabled()) {
            log.info("RoleController.add.bo:{}", JSON.toJSONString(authRoleBO));
        }
        AuthRole authRole = AuthRoleBoConvert.INSTANCE.authRoleBoToRole(authRoleBO);
        authRole.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int count = authRoleService.insert(authRole);
        return count > 0;
    }

    /**
     * 更新角色
     * @param authRoleBO
     * @return
     */
    @Override
    public Boolean update(AuthRoleBO authRoleBO) {
        if(log.isInfoEnabled()) {
            log.info("RoleController.update.bo:{}", JSON.toJSONString(authRoleBO));
        }
        AuthRole authRole = AuthRoleBoConvert.INSTANCE.authRoleBoToRole(authRoleBO);
        int count = authRoleService.update(authRole);
        return count > 0;
    }

    /**
     * 删除角色
     * @param authRoleBO
     * @return
     */
    @Override
    public Boolean delete(AuthRoleBO authRoleBO) {
        if(log.isInfoEnabled()) {
            log.info("RoleController.delete.bo:{}", JSON.toJSONString(authRoleBO));
        }
        AuthRole authRole = AuthRoleBoConvert.INSTANCE.authRoleBoToRole(authRoleBO);
        authRole.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = authRoleService.update(authRole);
        return count > 0;
    }
}
